/**
 * Created by zyx on 2017/9/18.
 */
angular.module('module.auth')
    .controller('RegisterController', RegisterController);

RegisterController.$inject = ['$state', 'AuthService'];

// 注册
function RegisterController($state, AuthService) {
    var vm = this;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    vm.checkPhone = checkPhone;
    vm.getCode = getCode;
    vm.confirmPwd = confirmPwd;
    vm.register = register;
    active();

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    function active(){
        $("#delete1").click(function () {
            document.getElementById("phone").value = "";
            $("#regist").attr("disabled");
        });
        $("#delete2").click(function () {
            document.getElementById("password").value = "";
            $("#regist").attr("disabled");
        });
    }

    // 确认手机号码
    function checkPhone() {
        var judge = /^1(3|4|5|7|8)\d{9}$/;
        if (vm.phone == "" || vm.phone == undefined) {
            $("#code").attr("disabled");
        } else if (!(judge.test(vm.phone))) {
            modal(true, "该手机号不合法！");
        } else {
            AuthService.getExit(vm.phone).$promise.then(function (data) {
                if (data.result == "该手机号已注册") {
                    modal(true, "该手机号已注册！");
                } else {
                    $("#code").removeAttr("disabled");
                }
            }, function (error) {

            })
        }
    }

    // 获取验证码
    function getCode() {
        var thisBtn = document.getElementById("code");
        AuthService.getExit(vm.phone).$promise.then(function (data) {
            if (data.result == "已存在") {
                modal(true, "该手机号已注册！");
            } else {
                sendCode(thisBtn);
                AuthService.getCode(vm.phone).$promise.then(function (data) {
                    if (data.result == "获取短信成功") {
                        $("#regist").removeAttr("disabled");
                    }
                })
            }
        }, function (error) {

        });
    }

    // 验证密码
    function confirmPwd() {
        var code = vm.user.code;
        if ((code == "" || code == undefined) && (vm.user.password == "" || vm.user.password == undefined)) {
            modal(true, "请输入相应信息！");
        } else if (code == "" || code == undefined) {
            $("#regist").attr("disabled");
            modal(true, "请输入验证码！");
        } else if (vm.user.password == "" || vm.user.password == undefined) {
            $("#regist").attr("disabled");
            modal(true, "请输入密码！");
        } else {
            $("#regist").removeAttr("disabled");
        }
    }

    // 注册按钮
    function register() {
        vm.user = {
            username: vm.phone,
            password: vm.user.password,
            phone: vm.phone,
            code: vm.user.code,
            confirmpassword: vm.user.password,
            role: "",
            email: ""
        };
        AuthService.register(vm.user).$promise.then(function (user) {
            modal(true, "注册成功！");
            $state.go("login");
        }, function (error) {
            modal(true, "注册失败！");
            console.log(error)
        })
    }

    // 提示模态框控制
    function modal(status, msg) {
        vm.messageVisible = status;
        vm.message = msg;
    }
}